//
// Created by Administrator on 2018/6/19.
//
#include "log.h"
#include "utils/device.h"
#include <sys/system_properties.h>
#include <string.h>

jstring getUnique(JNIEnv *env, jobject thiz, jobject context)
{
    jclass resCls = (*env)->GetObjectClass(env, context);
    jmethodID getMethod = (*env)->GetMethodID(env, resCls, "getContentResolver",
                                              "()Landroid/content/ContentResolver;");
    jobject resolver = (*env)->CallObjectMethod(env, context, getMethod);

    jclass cls_context = (*env)->FindClass(env, "android/provider/Settings$Secure");

    jmethodID getStringMethod = (*env)->GetStaticMethodID(env, cls_context, "getString",
                                                          "(Landroid/content/ContentResolver;Ljava/lang/String;)Ljava/lang/String;");

    jfieldID ANDROID_ID = (*env)->GetStaticFieldID(env, cls_context,
                                                   "ANDROID_ID", "Ljava/lang/String;");
    jstring str = (jstring) ((*env)->GetStaticObjectField(env, cls_context, ANDROID_ID));

    jstring jId = (jstring) ((*env)->CallStaticObjectMethod(env, cls_context, getStringMethod, resolver, str));
    char *szDevId = (char *) ((*env)->GetStringUTFChars(env, jId, 0));

    jclass buildClass = (*env)->FindClass(env, "android/os/Build");
    jfieldID fingerprintid = (*env)->GetStaticFieldID(env, buildClass, "FINGERPRINT", "Ljava/lang/String;");
    jstring fingerprint = (*env)->GetStaticObjectField(env, buildClass, fingerprintid);
    char *pFinger_print = (char *) (*env)->GetStringUTFChars(env, fingerprint, 0);

    char buffer[512];
    //android P 不可用
    __system_property_get("ro.serialno", buffer);
    strcat(buffer, szDevId);
    strcat(buffer, pFinger_print);
    return (*env)->NewStringUTF(env, buffer);
}
